package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Salary;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.SalaryQuery;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISalaryService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/salary")
public class SalaryController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISalaryService salaryService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/list.do")
    public String list(Model model, @ModelAttribute("qo")SalaryQuery qo){
        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees",employees);
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.listAll();
        model.addAttribute("systemDictionaryItems",systemDictionaryItems);
        PageInfo result = salaryService.query(qo);
        model.addAttribute("result",result);
        return "/salary/list";
    }
    @RequestMapping("/saveOrUpdate.do")
    @ResponseBody
    public JsonResult saveOrUpdate(Salary salary) {
        try {
            if (salary.getId() != null) {
                salaryService.update(salary);
            } else {
                salaryService.insert(salary);
            }
            //没有把数据响应给浏览器
            return new JsonResult(true, "新增或修改成功");
        } catch (Exception e) {
            //若出现异常
            return new JsonResult(false, "新增或者修改失败");
        }
    }

}
